package com.site.wisdom_construction.controller.iotModule;

import com.alibaba.fastjson.JSONObject;
import com.site.wisdom_construction.entity.personnelModule.ContractWorkers;
import com.site.wisdom_construction.entity.personnelModule.ManagerialStaff;
import com.site.wisdom_construction.responseEntity.FaceRecognitionResult;
import com.site.wisdom_construction.service.iotModule.RealTimePersonNewService;
import com.site.wisdom_construction.service.personnelModule.ContractWorkersService;
import com.site.wisdom_construction.service.personnelModule.ManagerialStaffService;
import com.site.wisdom_construction.util.imgUtil.Base64ToOss;
import com.site.wisdom_construction.util.verification.NoToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author daiyifei @Date 2022/3/10 @Description
 */
@Api(tags = "人脸识别闸机数据接口")
@RestController
@RequestMapping("RealTimePersonNew")
@CrossOrigin
public class RealTimePersonNewController {
  @Autowired private RealTimePersonNewService realTimePersonNewService;

  @Autowired private ContractWorkersService contractWorkersService;

  @Autowired private ManagerialStaffService managerialStaffService;

  @ApiOperation(value = "增加人脸识别信息", notes = "")
  @PostMapping("/addPersonRecord")
  @NoToken
  public FaceRecognitionResult addPersonRecord(@RequestBody Map map) throws IOException {
    System.out.println("map:" + map);
    FaceRecognitionResult faceRecognitionResult = new FaceRecognitionResult();
    // map转JSONObject
    JSONObject jsonObject = new JSONObject(map);
    // 获取其中的data
    JSONObject jsonObjectData = jsonObject.getJSONObject("match");
    // base64转图片上传oss
    String header = "data:image/jpg;base64,";
    String base64 = header + jsonObjectData.getString("image");
    // base64上传oss工具类
    Base64ToOss base64ToOSS = new Base64ToOss();
    String Host = "ZJDQJ/rlsbsj";

    Integer a = (Integer) jsonObjectData.get("match_result");

    //判断人脸比对结果,大于0判断人员是否存在
    if (a > 0) {
      // 传参map
      Map mapR = new HashMap();
      // 大桥局标段id
      mapR.put("projectId", 1);
      mapR.put("personId", jsonObjectData.get("person_id"));
      mapR.put("personName", jsonObjectData.get("person_name"));
      mapR.put("addrName", jsonObject.get("addr_name"));
      mapR.put("addrNo", jsonObject.get("addr_no"));
      mapR.put("capTime", jsonObject.get("cap_time"));
      // 返回oss图片路劲地址
      String imgUrl = base64ToOSS.GenerateImage(base64, Host);
      mapR.put("passImage", imgUrl);
      //根据personId查询劳务人员和管理人员是否存在
      ContractWorkers contractWorkers = contractWorkersService.findContractWorkersByPersonId(mapR);
      ManagerialStaff managerialStaff = managerialStaffService.findManagerialStaffByPersonId(mapR);
        // 进口设备序列号
        String device_sn = (String) jsonObject.get("device_sn");
        // 判断是否是进口的人脸识别机
        if (device_sn.equals("") || device_sn.equals("")) {
          //判断人员是否离场
          if(managerialStaff.getSpotStatus().equals("0") || contractWorkers.getSpotStatus().equals("0")){
            realTimePersonNewService.insertRealTimePersonNew(mapR);

            faceRecognitionResult.setReply("ACK");
            faceRecognitionResult.setCmd("face");
            faceRecognitionResult.setCode(0);
            faceRecognitionResult.setSequence_no((Integer) jsonObject.get("sequence_no"));
            faceRecognitionResult.setCap_time((String) jsonObject.get("cap_time"));

            JSONObject jsonObjectR = new JSONObject();
            jsonObjectR.put("device_type", "gpio");
            jsonObjectR.put("person_id", jsonObjectData.get("person_id"));
            jsonObjectR.put("ctrl_mode", "force");
            faceRecognitionResult.setGateway_ctrl(jsonObjectR);
          }else {
            faceRecognitionResult.setReply("ACK");
            faceRecognitionResult.setCmd("face");
            faceRecognitionResult.setCode(0);
            faceRecognitionResult.setSequence_no((Integer) jsonObject.get("sequence_no"));
            faceRecognitionResult.setCap_time((String) jsonObject.get("cap_time"));
          }
        }else {
          //判断是出口的人脸识别机
          //判断人员在场状态为在场
          if(managerialStaff.getSpotStatus().equals("1") || contractWorkers.getSpotStatus().equals("1")){
            realTimePersonNewService.insertRealTimePersonNew(mapR);

            faceRecognitionResult.setReply("ACK");
            faceRecognitionResult.setCmd("face");
            faceRecognitionResult.setCode(0);
            faceRecognitionResult.setSequence_no((Integer) jsonObject.get("sequence_no"));
            faceRecognitionResult.setCap_time((String) jsonObject.get("cap_time"));

            JSONObject jsonObjectR = new JSONObject();
            jsonObjectR.put("device_type", "gpio");
            jsonObjectR.put("person_id", jsonObjectData.get("person_id"));
            jsonObjectR.put("ctrl_mode", "force");
            faceRecognitionResult.setGateway_ctrl(jsonObjectR);
          }else {
            faceRecognitionResult.setReply("ACK");
            faceRecognitionResult.setCmd("face");
            faceRecognitionResult.setCode(0);
            faceRecognitionResult.setSequence_no((Integer) jsonObject.get("sequence_no"));
            faceRecognitionResult.setCap_time((String) jsonObject.get("cap_time"));
          }
        }

    } else {
      faceRecognitionResult.setReply("ACK");
      faceRecognitionResult.setCmd("face");
      faceRecognitionResult.setCode(0);
      faceRecognitionResult.setSequence_no((Integer) jsonObject.get("sequence_no"));
      faceRecognitionResult.setCap_time((String) jsonObject.get("cap_time"));
    }
    return faceRecognitionResult;
  }
}
